package com.xzgj.fmmall.service.impl;

import com.xzgj.fmmall.dao.ProductImgMapper;
import com.xzgj.fmmall.dao.ProductMapper;
import com.xzgj.fmmall.dao.ProductParamsMapper;
import com.xzgj.fmmall.dao.ProductSkuMapper;
import com.xzgj.fmmall.entity.Product;
import com.xzgj.fmmall.entity.ProductImg;
import com.xzgj.fmmall.entity.ProductParams;
import com.xzgj.fmmall.entity.ProductSku;
import com.xzgj.fmmall.service.ProductService;
import com.xzgj.fmmall.vo.ProductVO;
import com.xzgj.fmmall.vo.ResStatus;
import com.xzgj.fmmall.vo.ResultVO;
import com.xzgj.fmmall.vo.utils.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductImgMapper productImgMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;
    @Resource
    private ProductParamsMapper productParamsMapper;

    public ResultVO selectAllProducts(){
        List<ProductVO> ptvos = productMapper.selectRecommendProducts();
        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,ptvos);

    }

        public ResultVO selectRecommendProducts(int productId){
        List<ProductVO> ptvos = productMapper.selectRecommendProducts();
        for (ProductVO p:ptvos){
            System.out.println(p);
            p.setImgs(productImgMapper.selectProductImgByProductId(1));
            System.out.println("----" + p);
        }
        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,ptvos);

    }

    @Override
    public ResultVO selectLatestRecommendProducts() {
        List<ProductVO> ptvos = productMapper.selectLatestRecommendProducts();
        for (ProductVO p:ptvos){
            System.out.println(p);
            p.setImgs(productImgMapper.selectProductImgByProductId(1));
            System.out.println("----" + p);
        }
        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,ptvos);

    }

    @Override
    public ResultVO selectTopProductsOnSoldNumEveryCategory(int rootCategoryId) {
        List<ProductVO> ptvos = productMapper.selectTopProductsOnSoldNumEveryCategory(rootCategoryId);
        for (ProductVO p:ptvos){
            System.out.println(p);
            p.setImgs(productImgMapper.selectProductImgByProductId(1));
            System.out.println("----" + p);
        }
        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,ptvos);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    //整个都是查询操作，因此将隔离级别设置为SUPPORTS---事务的传播机制：默认级别-可重复读;
    // 不需要事务，但是如果说有事无调用，也加入到事务的步骤中来，事务的传播机制
    public ResultVO getProductsBasicInfo(String productId) {
        //1.商品的基本信息
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("product_id",productId);
        criteria.andEqualTo("product_status",1);
        //商品的状态product_status=1很重要,状态为1表示是上架商品
        List<Product> products = productMapper.selectByExample(example);
        if (products.size() > 0){//根据id查到了商品
            //2.商品的图片
            Example exampleImg = new Example(ProductImg.class);
            Example.Criteria criteriaImg = exampleImg.createCriteria();
            criteriaImg.andEqualTo("item_id",productId);
            List<ProductImg> productImgs = productImgMapper.selectByExample(exampleImg);
            //3.商品的套餐
            Example exampleSku = new Example(ProductSku.class);
            Example.Criteria criteriaSku = exampleSku.createCriteria();
            criteriaSku.andEqualTo("product_id",productId);
            criteriaSku.andEqualTo("status",1);
            List<ProductSku> productSkus = productSkuMapper.selectByExample(exampleSku);
            //要装入 商品、图片、套餐 三个信息，因此使用map来组合
            HashMap<String,Object> basicInfo = new HashMap<>();
            basicInfo.put("product",products.get(0));
            basicInfo.put("productImgs",productImgs);
            basicInfo.put("productSkus",productSkus);

            return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,basicInfo);
        }

        return new ResultVO(ResStatus.NO,ResStatus.productIsNotExist,null);
    }

    @Override
    public ResultVO getProductsParamsByProductId(String productId) {
        Example exampleParam = new Example(ProductParams.class);
        Example.Criteria criteriaParam = exampleParam.createCriteria();
        criteriaParam.andEqualTo("product_id",productId);
        List<ProductParams> productParams = productParamsMapper.selectByExample(exampleParam);
        if (productParams.size() > 0){
            return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,productParams.get(0));

        }else {//三无商品
            return new ResultVO(ResStatus.OK,ResStatus.productIsNotIdentify,null);

        }
    }

    @Override
    //使用自定义的分页器PageHelper
    public ResultVO getProductsByCategoryId(int categoryId, int pageNo, int pageSize) {
        //1.查询分页数据
        int start = (pageNo - 1) * pageSize;
        List<ProductVO> productVOS = productMapper.selectProductByCategoryId(categoryId,start,pageSize);
        //2.查询当前类别下的商品的总记录数
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("category_id",categoryId);
        int count = productMapper.selectCountByExample(example);
        //2.计算总页数
        int pageCount = count%pageSize==0?count/pageSize : count/pageSize+1;
        PageHelper<ProductVO> pageHelper = new PageHelper<ProductVO>(count,pageCount,productVOS);
        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,pageHelper);
    }

    @Override
    public ResultVO listBrands(int categoryId) {
        List<String> brands = productMapper.selectBrandsByCategoryId(categoryId);
        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,brands);
    }

    @Override
    public ResultVO searchProducts(String keyword, int pageNo, int pageSize) {
        //1.查询送搜索结果---因为页面数的时候没有%，所以这里要拼接
        keyword = "%"+keyword+"%";
        int start = (pageNo - 1) * pageSize;
        List<ProductVO> productVOS = productMapper.selectProductByKeyword(keyword,start,pageSize);
        //2.查询总记录数
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("product_name",keyword);
        int count = productMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count%pageSize == 0? count/pageSize:count/pageSize+1;
        //4.封装,返回数据---做自定义分页功能
        PageHelper<ProductVO> pageHelper = new PageHelper<ProductVO>(count,pageCount,productVOS);
        ResultVO resultVO = new ResultVO(ResStatus.OK,ResStatus.SUCCESS,pageHelper);
        return resultVO;
    }

    @Override
    public ResultVO listBrands(String keyword) {
        keyword = "%"+keyword+"%";
        List<String> brands = productMapper.selectBrandsByKeyword(keyword);
        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,brands);
    }

    @Override
    public ResultVO selectLowestPriceByProductId(String productId) {
        List<ProductSku> productSkus = productSkuMapper.selectLowestPriceByProductId(productId);

        if (productSkus.size() == 0){
            return new ResultVO(ResStatus.NO,ResStatus.productSkuIsNotExists,null);
        }else {
            return new ResultVO(ResStatus.OK,ResStatus.operationSuccess,productSkus);
        }
    }
}
